// pages/gangManage/productPutawayEdit/productPutawayEdit.js
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')

Page({
  data: {
    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 300,
    specifications: {},
    data: {},
    product_id: '',
    gangRole: 0,
    classify: 0,
    passageCount: 0
  },
  onLoad: function(option) {
    var that = this
    that.setData({
      product_id: option.product_id
    })
    http.post({
      url: "product/getDetail",
      product_id: that.data.product_id,
      sold_out: 1
    }, function(data) {
      console.log('未上架商品详情', data)
      var defaultPrice = []
      for (var i in data.specifications) {
        defaultPrice.push({
          'gang_price': data.specifications[i]['gang_price'],
          'retail_price': data.specifications[i]['retail_price']
        })
      }
      that.setData({
        data: data,
        specifications: data.specifications,
        defaultPrice: defaultPrice,
        passageCount: data.passage_count,
      })
    })

  },
  bindEditPrice(e) {
    var index = e.currentTarget.dataset.index
    var stype = e.currentTarget.dataset.type
    var specifications = this.data.specifications
    var defaultPrice = this.data.defaultPrice
    specifications[index][stype] = e.detail.value
    var lowerLimit = 1
    if (stype == 'gang_price' || stype == 'retail_price') {
      lowerLimit = defaultPrice[index][stype]
    }
    if (specifications[index][stype] <= lowerLimit) {
      specifications[index][stype] = lowerLimit
      specifications[index][stype + "_disabled"] = true
    }
    this.setData({
      specifications: specifications
    })
    this.setData({
      specifications: specifications
    })
  },
  sub: function(e) {
    var index = e.currentTarget.dataset.index
    var stype = e.currentTarget.dataset.type
    var specifications = this.data.specifications
    var defaultPrice = this.data.defaultPrice
    console.log(stype)
    if (typeof(specifications[index][stype]) == "undefined")
      specifications[index][stype] = 0
    specifications[index][stype]--
      var lowerLimit = 1
    if (stype == 'gang_price' || stype == 'retail_price') {
      lowerLimit = defaultPrice[index][stype]
    }
    if (specifications[index][stype] <= lowerLimit) {
      specifications[index][stype] = lowerLimit
      specifications[index][stype + "_disabled"] = true
    }
    specifications[index][stype] = Math.round(specifications[index][stype] * 100) / 100
    this.setData({
      specifications: specifications
    })
  },
  add: function(e) {
    var index = e.currentTarget.dataset.index
    var stype = e.currentTarget.dataset.type
    var specifications = this.data.specifications
    if (typeof(specifications[index][stype]) == "undefined")
      specifications[index][stype] = 0
    specifications[index][stype]++
    specifications[index][stype + "_disabled"] = false
    specifications[index][stype] = Math.round(specifications[index][stype] * 100) / 100
    this.setData({
      specifications: specifications
    })
  },
  setPassageCount: function(e) {
    this.setData({
      passageCount: e
    })
  },
  formSubmit: function(e) {
    var that = this
    var pics = [];
    for (p in that.data.imgs) {
      pics.push(that.data.imgs[p].src)
    }
    if (that.data.product_id) {
      http.post({
        url: "product/putaway",
        product_id: that.data.product_id,
        specifications: that.data.specifications,
      }, function(data) {
        util.showSuccess('上架成功', function() {
          var pages = getCurrentPages();
          if (pages.length > 1) {
            var prePage = pages[pages.length - 2];
            prePage.getData()
          }
          wx.navigateBack({
            delta: 1
          })
        })
      })
    }
  }
})